﻿using PeaceBlog.Models.Types;
using PeaceBlogData;
using PeaceBlogData.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace PeaceBlog.Controllers.Web.Admin
{
    public class BlogAdminController : Controller
    {
        private PeaceBlogEntity db = new PeaceBlogEntity();

        private API.PostsController Posts = new API.PostsController();
        private API.AuthorsController Authors = new API.AuthorsController();
        private API.CategoriesController Categories = new API.CategoriesController();
        private API.PagesController Pages = new API.PagesController();

        #region General

        // GET: BlogAdmin
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid && model.Login())
            {
                return RedirectToAction("Dashboard");
            }
            else
                AddMessage("Kullanıcı Adınız veya Şifreniz Hatalıdır!", MessageType.Error);
            return View(model);
        }

        // GET: BlogAdmin/Logout
        public ActionResult Logout()
        {
            return RedirectToAction("Login");
        }

        // GET: BlogAdmin/DashBoard
        public ActionResult DashBoard()
        {
            return View();
        }

        #endregion

        #region Posts

        // GET: Posts
        public async Task<ActionResult> PostList()
        {
            return View(await Posts.GetPosts());
        }

        // GET: Posts/Details/5
        public async Task<ActionResult> PostDetail(Guid? id)
        {
            if (!id.HasValue)
                AddMessage("", MessageType.Error);
            var posts = await Posts.GetPost(id.Value);
            if (posts == null)
                return HttpNotFound();
            return View(posts);
        }

        // GET: Posts/Create
        public ActionResult AddPost()
        {
            return View();
        }

        // POST: Posts/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> AddPost(PeaceBlogData.Tables.Posts posts)
        {
            if (!ModelState.IsValid)
            {
                AddMessage("", MessageType.Error);
                return View(posts);
            }
            await Posts.AddPost(posts);
            return View(posts);
        }

        // GET: Posts/Edit/5
        public async Task<ActionResult> EditPost(Guid? id)
        {
            if (id.HasValue)
                AddMessage("", MessageType.Error);
            var posts = await Posts.GetPost(id.Value);
            if (posts == null)
                AddMessage("", MessageType.Error);
            return View(posts);
        }

        // POST: Posts/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> EditPost(PeaceBlogData.Tables.Posts posts)
        {
            if (!ModelState.IsValid)
            {
                AddMessage("", MessageType.Error);
                return View(posts);
            }
            await Posts.UpdatePost(posts);
            return View(posts);
        }
        
        #endregion

        #region Authors

        // GET: Authors
        public async Task<ActionResult> AuthorList()
        {
            return View(await Authors.GetAuthors());
        }

        // GET: Authors/Details/5
        public async Task<ActionResult> AuthorDetail(int? id)
        {
            if (!id.HasValue)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var authors = await Authors.GetAuthors(id.Value);
            if (authors == null)
            {
                return HttpNotFound();
            }
            return View(authors);
        }

        // GET: Authors/Create
        public ActionResult AddAuthor()
        {
            return View();
        }

        // POST: Authors/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> AddAuthor(Authors authors,bool ResimYok)
        {
            if (ModelState.IsValid)
            {
                if (Request.Files.Count > 0)
                {

                }
                await Authors.AddAuthors(authors);
                AddMessage("", MessageType.Success);
                return View(authors);
            }
            else
                AddMessage("", MessageType.Error);

            return View(authors);
        }

        // GET: Authors/Edit/5
        public async Task<ActionResult> EditAuthor(int? id)
        {
            if (!id.HasValue)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var authors = await Authors.GetAuthors(id.Value);
            if (authors == null)
            {
                return HttpNotFound();
            }
            return View(authors);
        }

        // POST: Authors/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> EditAuthor(Authors authors)
        {
            if (ModelState.IsValid)
            {
                await Authors.UpdateAuthors(authors);
                AddMessage("", MessageType.Success);
            }
            else
                AddMessage("", MessageType.Error);
            return View(authors);
        }

        // GET: Authors/Delete/5
        public async Task<ActionResult> DeleteAuthor(int? id)
        {
            if (!id.HasValue)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var authors = await Authors.GetAuthors(id.Value);
            if (authors == null)
            {
                return HttpNotFound();
            }
            return View(authors);
        }

        // POST: Authors/Delete/5
        [HttpPost]
        public async Task<ActionResult> DeleteAuthor(int id)
        {
            await Authors.DeleteAuthors(id);
            return RedirectToAction("AuthorList");
        }

        #endregion

        #region Categories

        // GET: Categories
        public async Task<ActionResult> CategoryList()
        {
            return View(await Categories.GetCategories());
        }

        // GET: Categories/Details/5
        public async Task<ActionResult> CategoryDetail(int? id)
        {
            if (!id.HasValue)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var categories = await Categories.GetCategory(id.Value);
            if (categories == null)
            {
                return HttpNotFound();
            }
            return View(categories);
        }

        // GET: Categories/Create
        public ActionResult AddCategory()
        {
            return View();
        }

        // POST: Categories/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> AddCategory(Categories categories)
        {
            if (ModelState.IsValid)
            {
                await Categories.PostCategories(categories);
                AddMessage("", MessageType.Success);
            }
            else
                AddMessage("", MessageType.Error);
            return View(categories);
        }

        // GET: Categories/Edit/5
        public async Task<ActionResult> EditCategory(int? id)
        {
            if (!id.HasValue)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var categories = await Categories.GetCategories(id.Value);
            if (categories == null)
            {
                return HttpNotFound();
            }
            return View(categories);
        }

        // POST: Categories/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> EditCategory(Categories categories)
        {
            if (ModelState.IsValid)
            {
                await Categories.UpdateCategory(categories);
                AddMessage("", MessageType.Success);
            }
            else
                AddMessage("", MessageType.Error);
            return View(categories);
        }

        // GET: Categories/Delete/5
        public async Task<ActionResult> DeleteCategory(int? id)
        {
            if (!id.HasValue)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var categories = await Categories.GetCategories(id.Value);
            if (categories == null)
            {
                return HttpNotFound();
            }
            return View(categories);
        }

        // POST: Categories/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteCategory(int id)
        {
            await Categories.DeleteCategories(id);
            return RedirectToAction("CategoryList");
        }

        #endregion

        #region Pages

        // GET: Pages
        public async Task<ActionResult> PageList()
        {
            return View(await Pages.GetPages());
        }

        // GET: Pages/Details/5
        public async Task<ActionResult> PageDetail(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var pages = await Pages.GetPages(id.Value);
            if (pages == null)
            {
                return HttpNotFound();
            }
            return View(pages);
        }

        // GET: Pages/Create
        public ActionResult AddPage()
        {
            return View();
        }

        // POST: Pages/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> AddPage(Pages pages)
        {
            if (ModelState.IsValid)
            {
                await Pages.AddPages(pages);
                AddMessage("", MessageType.Success);
            }
            else
                AddMessage("", MessageType.Success);

            return View(pages);
        }

        // GET: Pages/Edit/5
        public async Task<ActionResult> EditPage(Guid? id)
        {
            if (!id.HasValue)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var pages = await Pages.GetPages(id.Value);
            if (pages == null)
            {
                return HttpNotFound();
            }
            return View(pages);
        }

        // POST: Pages/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> EditPage(Pages pages)
        {
            if (ModelState.IsValid)
            {
                await Pages.UpdatePages(pages);
                AddMessage("", MessageType.Success);
            }
            else
                AddMessage("", MessageType.Error);
            return View(pages);
        }
        
        // POST: Pages/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeletePage(Guid id)
        {
            await Pages.DeletePages(id);
            return RedirectToAction("PageList");
        }
        
        #endregion

        #region Files

        #endregion

        #region Sliders

        #endregion

        #region Menus

        #endregion

        #region Widgets

        #endregion

        #region Stats

        #endregion

        #region Settings

        #endregion


        #region Helper

        public void AddMessage(string mesaj, MessageType type)
        {
            ModelState.AddModelError(type.GetStringValue(), mesaj);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
                Posts.Dispose();
                Authors.Dispose();
            }
            base.Dispose(disposing);
        }

        #endregion
    }
}